package com.bianguo.android.iocdemo.view.activity;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.transition.Explode;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.bianguo.android.injectlibrary.annotations.ContentView;
import com.bianguo.android.injectlibrary.annotations.InjectView;
import com.bianguo.android.injectlibrary.utils.MLog;
import com.bianguo.android.iocdemo.R;
import com.bianguo.android.iocdemo.adapter.SplashAdapter;
import com.bianguo.android.iocdemo.item.SplashItem;
import com.bianguo.android.iocdemo.view.widget.DragBubbleView;
import com.bianguo.android.recyclerlibrary.adapter.RViewAdapter;

import java.util.ArrayList;

@ContentView(R.layout.activity_splash)
public class SplashActivity extends BaseRVActivity {

    @InjectView(R.id.splash_recycler)
    RecyclerView mRecycler;
    @InjectView(R.id.splash_root)
    FrameLayout rootLayout;
    @InjectView(R.id.splash_title_layout)
    LinearLayout titleLayout;

    private int titleLayoutHeight;
    private int scrollDistance;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Window window = getWindow();
        //允许使用transitions过渡动画
        window.requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
        window.setEnterTransition(new Explode());
        super.onCreate(savedInstanceState);
        mRecycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (titleLayoutHeight == 0) {
                    titleLayoutHeight = titleLayout.getHeight();
                }
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (titleLayoutHeight == 0) {
                    return;
                }
                LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                scrollDistance = getScollYDistance(layoutManager);
                scrollDistance = scrollDistance > titleLayoutHeight ? titleLayoutHeight : scrollDistance;
                scrollDistance = scrollDistance < 0 ? 0 : scrollDistance;
                titleLayout.setAlpha(scrollDistance * 1.0f / titleLayoutHeight);
            }
        });
    }

    public int getScollYDistance(LinearLayoutManager layoutManager) {
        int position = layoutManager.findFirstVisibleItemPosition();
        View firstVisiableChildView = layoutManager.findViewByPosition(position);
        int itemHeight = firstVisiableChildView.getHeight();
        return (position) * itemHeight - firstVisiableChildView.getTop();
    }

    @Override
    public void onRefresh() {

    }

    @Override
    public RecyclerView createRecyclerView() {
        return mRecycler;
    }

    @Override
    public RViewAdapter createRViewAdapter() {
        ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i < 30; i++) {
            list.add("item --> " + i);
        }
        SplashAdapter adapter = new SplashAdapter(list);
        adapter.addItemStyle(new SplashItem());
        return adapter;
    }
}
